﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Objects;

namespace Nexterday.Linq.Autocompiler.EF4
{
    public static class Extensions
    {
        public static IQueryable<T> AsCompilable<T>(this ObjectQuery<T> query) where T : class
        {
            return AsCompilable(query, CompilationParameters.Default);
        }
        public static IQueryable<T> AsCompilable<T>(this ObjectQuery<T> query, CompilationParameters parameters) where T : class
        {
            var queryType=typeof(CompilableQuery<,>).MakeGenericType(query.Context.GetType(),typeof(T));
            return Activator.CreateInstance(queryType, query, parameters) as IQueryable<T>;
        }

        public static IQueryable<TEntity> GetCompiled<TContext, TEntity>(this TContext context, Func<TContext, ObjectQuery<TEntity>> query) where TContext : ObjectContext
        {
            return new CompilableQuery<TContext, TEntity>(query.Invoke(context), CompilationParameters.Default);
        }

        public static IQueryable<TEntity> GetCompiled<TContext, TEntity>(this TContext context, Func<TContext, ObjectQuery<TEntity>> query, CompilationParameters parameters) where TContext : ObjectContext
        {
            return new CompilableQuery<TContext, TEntity>(query.Invoke(context), parameters);
        }

    }
}
